package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.FinancingProduct;

public class FinancingProductDao extends BaseDao{
    public Connection conn;
    public PreparedStatement pstmt;
    public ResultSet rs;

	public List<FinancingProduct> getList(){
		List<FinancingProduct> list=new ArrayList<FinancingProduct>();
		try {
			conn=super.getConnection();
			String sql="SELECT  * FROM financingproduct ";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				FinancingProduct fin=new FinancingProduct();
				fin.setId(rs.getString("id"));
				fin.setRisk(rs.getInt("risk"));
				fin.setIncome(rs.getString("income"));
				fin.setSaleStaring(rs.getString("saleStarting"));
				fin.setSaleEne(rs.getString("saleEnd"));
				fin.setEnd(rs.getString("end"));
				list.add(fin);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;

	}
	public int getId(String id){
		int reslt=0;
		
		try {
			conn=super.getConnection();
			String sql="SELECT COUNT(1) FROM financingproduct WHERE id=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,id);
			rs = pstmt.executeQuery();
			if(rs.next()){
				reslt=rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return reslt;

	}
	public int addFin(FinancingProduct fin){
		int restl=0;
		try {
			conn=super.getConnection();
			String sql="INSERT into financingproduct(id,risk,income,saleStarting,saleEnd,end) VALUES(?,?,?,?,?,?) ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setObject(1,fin.getId());
			pstmt.setObject(2,fin.getRisk());
			pstmt.setObject(3,fin.getIncome());
			pstmt.setObject(4,fin.getSaleStaring());
			pstmt.setObject(5,fin.getSaleEne());
			pstmt.setObject(6,fin.getEnd());
			restl = pstmt.executeUpdate();	
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return restl;


	}
	public List<FinancingProduct> Listlike(String id,int risk){
		List<FinancingProduct> list=new ArrayList<FinancingProduct>();
		try {
			conn=super.getConnection();
			String sql="SELECT * FROM financingproduct WHERE id LIKE '%"+id+"%'and risk =?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, risk);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				FinancingProduct fin=new FinancingProduct();
				fin.setId(rs.getString("id"));
				fin.setRisk(rs.getInt("risk"));
				fin.setIncome(rs.getString("income"));
				fin.setSaleStaring(rs.getString("saleStarting"));
				fin.setSaleEne(rs.getString("saleEnd"));
				fin.setEnd(rs.getString("end"));
				list.add(fin);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;
		
	}
}
